/**
 * @author yangbing
 * @time 2022-07-01 16PM
 * @description 路由配置
 */
import Vue from 'vue'
import Router from 'vue-router'
import Store from '../store'

Vue.use(Router)

//这里引入我们的路由组件
// import Login from '../pages/login'
// import Home from '../pages/home'
// import NotFound from '../pages/common/NotFound'

//这里是重写push方法
const pushPlugin = Router.prototype.push
Router.prototype.push = function(params) {
    pushPlugin.call(this, params).catch(e => {
        console.warn('跳转了同样的路由')
    })
}


//动态引入组件，懒加载
const Login = function(){
    // return import('../pages/login/login.vue')
    return import('../pages/login')
}
const Home = ()=> import('../pages/home')
const NotFound = ()=> import('../pages/common/NotFound')
const UserEdit = ()=> import('../pages/user/edit')
const UserList = ()=> import('../pages/user/list')
const menulist = ()=> import('../pages/system/menu.vue')
//实例化路由
const router = new Router({
    //路由模式
    mode:'history',
    //配置路由属性
    routes:[
        {path:'/',redirect:'/login'},
        {path:'/login',component:Login,meta:{nologin:true}},
        {path:'/home',component:Home},
        {path:'/edit',component:UserEdit},
        {path:'/list',component:UserList},
        {path:'*',component:NotFound,meta:{nologin:true}},
        {path:'/menulist',component:menulist},

    ]
})

//全局拦截
router.beforeEach((to,from,next)=>{
    if(to.meta.nologin){
        next()
        return
    }

    //注意 这里getter取得是一个数组 不是一个方法
    //获取token判断
    let token = Store.getters['common/token']
    if(!token){
        next('/login')
        return
    }
    //获取权限
    let right = true
    if(!right){
        alert('你没有权限，请联系管理员xxx')
        return
    }
    next()
})
//导出
export default router
